'''
Created on Jan 22, 2010

@author: maasen
'''
#this is for debugging when not running on server / 
#the path extension is not needed when django project is live 
#as the common paths get integrated
import sys
sys.path.append("../../common/zip-packages/freebase.zip")

import freebase
import urllib

user = 'maasen'
pword = 'maasen@freeform'

#Wrapper for writing to freebase, includes login/logout
def freebase_write_sandbox(uname, pword, query):
    freebase.sandbox.login(username=uname, password=pword)
    res = freebase.sandbox.mqlwrite(query)
    freebase.sandbox.logout()
    return res
#Query freebase
def freebase_read(query):
    results = freebase.mqlread(query)
    return results
#source_guid: The guid of the topic
#guid_dest: The guid of the topic being linked to
#type: The type of the topic being linked to
def freebase_link_topics(guid_source, type, guid_dest):
    query = {
             'id' : guid_source,
             type :{
                    'connect':'insert',
                    'id':guid_dest   
                    }
             }
    res = freebase_write_sandbox(user, pword, query)
    return res
#Creates a new Medical Trial in freebase
#CT_ID: The Clinical Trial ID
#returns the response
#def freebase_create_new_MT(CT_ID):
#    create_MT_query = {
#                       'create' : 'unless_exists',
#                       'type'  : '/medicine/medical_trial',
#                       'clinicaltrials_identifier' : CT_ID,
#                       'id' : None
#                       }
#
#    res = freebase_write_sandbox(user, pword, create_MT_query)
#    return res


#Creates a new topic in Freebase
#type: The type of topic to create
#field: The initial field you wish to fill out
#field_value: The value to add to the field
#returns the id for the topic (guid)
def freebase_create_new_topic(type, field, field_value):
    create_MT_query = {
                       'create' : 'unless_exists',
                       'type'  : type,
                       field : field_value,
                       'id' : None
                       }

    res = freebase_write_sandbox(user, pword, create_MT_query)
    return res['id']

#Adds a title to a medical trial
#source_guid: The guid of the topic
#field_name: The name of the field (likely 'name' as this method didn't work as a generic method
#field_value: The CT title
def freebase_add_text_field(source_guid, field_name, field_value):
    query = {
             'id': source_guid,
             field_name:{
                         'connect':'update',
                         'value':field_value,
                         'lang':'/lang/en'
                         }
             }

    res = freebase_write_sandbox(user, pword, query)
    return res;

def freebase_get_id(type, field, value):
    get_link_ID = {
                   'type' : type,
                   field : value,
                   'id': None
                   }
    res = freebase_read(get_link_ID)
    print "Got ID for study design type: '" + value + "'"

    return res['id']
        
